/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.shop.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luciad.imageio.webp.WebPReadParam;
import com.shop.cereshop.app.dao.location.CityDAO;
import com.shop.cereshop.app.dao.shop.CereBusinessTimeDAO;
import com.shop.cereshop.app.dao.shop.CerePlatformShopDAO;
import com.shop.cereshop.app.domain.shop.CereShopCommentVO;
import com.shop.cereshop.app.page.banner.ShopBanner;
import com.shop.cereshop.app.page.cart.CartSku;
import com.shop.cereshop.app.page.index.Product;
import com.shop.cereshop.app.page.index.RecommendShop;
import com.shop.cereshop.app.page.product.ProductCoupon;
import com.shop.cereshop.app.page.settlement.ShopInfo;
import com.shop.cereshop.app.page.shop.*;
import com.shop.cereshop.app.param.shop.ShopParam;
import com.shop.cereshop.app.param.shop.ShopPosterParam;
import com.shop.cereshop.app.param.shop.ShopQueryParam;
import com.shop.cereshop.app.service.buyer.CereBuyerSearchService;
import com.shop.cereshop.app.service.buyer.CereBuyerShopCouponService;
import com.shop.cereshop.app.service.collect.CereBuyerCollectService;
import com.shop.cereshop.app.service.distributor.CereShopDistributorService;
import com.shop.cereshop.app.service.product.CereShopProductService;
import com.shop.cereshop.app.service.shop.*;
import com.shop.cereshop.app.utils.AlipayUtil;
import com.shop.cereshop.app.utils.WechatUtil;
import com.shop.cereshop.commons.cache.product.ProductBo;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.buyer.CereBuyerSearch;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.calculate.Calculate;
import com.shop.cereshop.commons.domain.city.CereCityDetail;
import com.shop.cereshop.commons.domain.collect.CereBuyerCollect;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.domain.product.ProductVo;
import com.shop.cereshop.commons.domain.shop.CereBusinessTime;
import com.shop.cereshop.commons.domain.shop.CerePlatformShop;
import com.shop.cereshop.commons.domain.shop.CereShopReturn;
import com.shop.cereshop.commons.domain.shop.ShopVo;
import com.shop.cereshop.commons.enums.BusinessTypeEnum;
import com.shop.cereshop.commons.enums.ShopTypeEnum;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.upload.strategy.FileStrategy;
import com.shop.cereshop.commons.utils.BusinessWeeksUtils;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.QRCodeUtil;
import com.shop.cereshop.commons.utils.TimeUtils;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CerePlatformShopServiceImpl implements CerePlatformShopService {

    @Resource
    private CerePlatformShopDAO cerePlatformShopDAO;
    @Resource
    private CereShopReturnService cereShopReturnService;
    @Resource
    private CereShopOtherOrganizationsService cereShopOtherOrganizationsService;
    @Resource
    private CereShopEnterpriseService cereShopEnterpriseService;
    @Resource
    private CereShopPersonalService cereShopPersonalService;
    @Resource
    private CereShopIndividualBusinessesService cereShopIndividualBusinessesService;
    @Resource
    private CereShopBrandService cereShopBrandService;
    @Resource
    private CereBuyerCollectService cereBuyerCollectService;
    @Resource
    private CereBuyerShopCouponService cereBuyerShopCouponService;
    @Resource
    private CereShopProductService cereShopProductService;
    @Resource
    private CereShopDistributorService cereShopDistributorService;
    @Resource
    private FileStrategy fileStrategy;
    @Resource
    private CityDAO cityDAO;
    @Resource
    private CereBuyerSearchService cereBuyerSearchService;
    @Resource
    private CereBusinessTimeDAO cereBusinessTimeDAO;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 店铺页面前缀
     */
    @Value("${shop_url}")
    private String shopUrl;


    /**
     * 店铺招募页面前缀
     */
    @Value("${recruit_url}")
    private String recruitUrl;


    @Value("${query.params.product.shopList.tongcheng}")
    private Integer tongchengNumber;

    @Value("${query.params.product.shopList.xianshang}")
    private Integer xianshangNumber;

    @Override
    public List<RecommendShop> findRecommendShop() {
        return cerePlatformShopDAO.findRecommendShop();
    }

    @Override
    public RecommendShop findVolumeProductByShopId(Long shopId) {
        return cerePlatformShopDAO.findVolumeProductByShopId(shopId);
    }

    @Override
    public List<RecommendShop> findRecommendProducts() {
        return cerePlatformShopDAO.findRecommendProducts();
    }

    @Override
    public Integer findPayUsers(Long productId) {
        Integer payUsers = cerePlatformShopDAO.findPayUsers(productId);
        int fictitiousNumber = cereShopProductService.selectFictitiousNumber(productId);
        return payUsers + fictitiousNumber;
    }

    @Override
    public Shop getShopProducts(ShopParam param, CereBuyerUser user) throws CoBusinessException {
        //查询店铺已售件数
        Shop shop = cerePlatformShopDAO.findNumber(param.getShopId());
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Product> list = cerePlatformShopDAO.getShopProducts(param);
        if (!EmptyUtils.isEmpty(list)) {
            List<Long> skuIdList = list.stream().map(Product::getSkuId).collect(Collectors.toList());
            Map<Long, Product> productMap = cereShopProductService.getSimpleActivityInfo(param.getShopId(), skuIdList, user);
            for (Product product : list) {
                Product activityProduct = productMap.get(product.getSkuId());
                if (activityProduct != null) {
                    product.setActivityType(activityProduct.getActivityType());
                    product.setOriginalPrice(activityProduct.getOriginalPrice());
                    product.setPrice(activityProduct.getPrice());
                    product.setUsers(activityProduct.getUsers());
                }
            }
        }
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        shop.setPage(page);
        return shop;
    }

    @Override
    public List<ShopClassify> getShopClassify(ShopParam param) throws CoBusinessException {
        return cerePlatformShopDAO.getShopClassify(param.getShopId());
    }

    @Override
    public Page getExtensionProduct(ShopParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Product> list = cerePlatformShopDAO.getExtensionProduct(param);
        if (!EmptyUtils.isEmpty(list)) {
            list.forEach(a -> {
                //查询付款人数
                a.setUsers(findPayUsers(a.getProductId()));
                //设置活动信息
                ProductBo productBo = cereShopProductService.fetchProductCache(a.getShopId(), a.getProductId(), a.getSkuId(), null, false);
                if (productBo != null) {
                    a.setActivityType(productBo.getActivityType());
                    a.setOriginalPrice(productBo.getOriginalPrice());
                    a.setPrice(productBo.getPrice());
                    a.setTotal(productBo.getTotal());
                }
            });
        }
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    public List<ProductCoupon> findShopToolByProductId(Long shopId, Long productId) {
        return cerePlatformShopDAO.findShopToolByProductId(shopId, productId);
    }

    @Override
    public List<ShopBanner> getShopBanner(Long shopId) throws CoBusinessException {
        return cerePlatformShopDAO.getShopBanner(shopId);
    }

    @Override
    public Page getShops(ShopParam param, CereBuyerUser user) throws CoBusinessException {
        //过滤表情包参数
        param.setSearch(EmptyUtils.filterEmoji(param.getSearch()));
        if (!EmptyUtils.isEmpty(param.getSearch()) && user != null && param.getSearchType() != null) {
//            判断类型是否合法

            //查询当前搜索记录是否存在
            CereBuyerSearch cereBuyerSearch = cereBuyerSearchService.findBySearch(param.getSearch(), user.getBuyerUserId(), param.getSearchType());
            if (cereBuyerSearch == null) {
                //如果没有,新增搜索记录
                cereBuyerSearch = new CereBuyerSearch();
                cereBuyerSearch.setBuyerUserId(user.getBuyerUserId());
                cereBuyerSearch.setSearch(param.getSearch());
                cereBuyerSearch.setSearchType(param.getSearchType());
                cereBuyerSearch.setSearchType(2);
                cereBuyerSearchService.insert(cereBuyerSearch);
            }
        }

        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<CereShopCommentVO> list = cerePlatformShopDAO.getShops(param);
//        if (param.getShopType() != 3) {
//            Map<Long, Map<String, Object>> shopList = cerePlatformShopDAO.getInstanceToProduct(
//                    param.getCityCode(),    // 城市编码（缩小查询范围）
//                    param.getLocation() // 用户经纬度
//            );
//            //            取出计算用户到商家之间的距离是多少
//            for (CereShopCommentVO cereShopCommentVO : list) {
//                if (shopList.get(cereShopCommentVO.getShopId()) != null) {
////                    cereShopCommentVO.setUserAndShopDistance((Double.valueOf(shopList.get(cereShopCommentVO.getShopId()).get("distance")));
//                }
//            }
//        }


        if (!EmptyUtils.isEmpty(list)) {
            //获取前端传过来的城市编码
            String cityCode = param.getCityCode();

            //查询满足条件商品数据
            list.forEach(shop -> {

//                判断店铺是否是新店
                if (shop.getCurrentTime() != null) {
                    shop.setXinShop(isNewShop(shop.getCurrentTime()));

                }
                shop.setXinShopStr(shop.getXinShop());

                param.setShopId(shop.getShopId());
                List<CartSku> skus = cerePlatformShopDAO.findSkuByShopParam(param, shop.getType() == 1 ? tongchengNumber : xianshangNumber, param.getClassifyTwoId());
                shop.setSkus(skus);
                shop.setAuthenTypeStr(shop.getAuthenType());
                //  redisTemplate.opsForHash().put("shop", String.valueOf(shopId), collect.get(shopId).get(0));
                Object shopSignal = redisTemplate.opsForHash().get("shop", String.valueOf(shop.getShopId()));
                //Calculate
                if (null != shopSignal) {
                    Calculate calculate = JSONUtil.toBean(JSONObject.toJSONString(shopSignal), Calculate.class);
                    if (calculate != null) {
                        shop.setServiceLabels(calculate);
                    }
                }

                shop.setStatus(null);


                // 获取店铺的营业时间
                QueryWrapper<CereBusinessTime> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("shop_id", shop.getShopId());
                List<CereBusinessTime> bussinessTimes = cereBusinessTimeDAO.selectList(queryWrapper);

                // 添加空值检查
                if (bussinessTimes == null) {
                    bussinessTimes = new ArrayList<>();
                }
                shop.setShopWeekList(bussinessTimes);


                // 判断是否为全天
                boolean isAllDay = false;
                if (bussinessTimes != null && !bussinessTimes.isEmpty()) {
                    isAllDay = bussinessTimes.stream()
                            .filter(Objects::nonNull) // 过滤掉null元素
                            .allMatch(bt -> {
                                if (bt != null && bt.getStartTime() != null && bt.getEndTime() != null) {
                                    try {
                                        // 格式化时间为 HH:mm:ss 格式进行比较
                                        String startTimeStr = bt.getStartTime();
                                        String endTimeStr = bt.getEndTime();
                                        return "00:00:00".equals(startTimeStr) && "23:59:59".equals(endTimeStr);
                                    } catch (Exception e) {
                                        // 处理格式化异常
                                        return false;
                                    }
                                }
                                return false;
                            });
                }
                shop.setAllDayToUse(isAllDay ? 1 : 0); // 设置是否全天的标志
                // 设置店铺类型状态

                if (param.getShopType() != null && param.getCityCode() != null) {
                    ShopTypeEnum shopType = ShopTypeEnum.getByTypeAndAdcode(shop.getType(), shop.getAdcode(), cityCode);
                    if (shopType != null) {

                        switch (shopType) {
                            case TONG_CHENG_FU_WU:
                                // 同城服务
                                if (shop.getAdcode() != null && cityCode != null && shop.getAdcode().equals(cityCode)) {
                                    shop.setStatus(1);
                                } else {
                                    // 表示该店铺只支持同城服务，但又和该用户不在同一个城市就过滤掉
                                    shop.setStatus(0);
                                }
                                break;
                            case TONG_CHENG_PEI_SONG:
                                // 同城配送
                                shop.setStatus(3);
                                break;
                            case XIAN_SHANG_SHANG_CHENG:
                                // 线上商城
                                shop.setStatus(2);
                                break;
                        }
                    }
                }
            });

            // 过滤掉status为0的店铺
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != null && list.get(i).getStatus() != null) {
                    list = list.stream()
                            .filter(shop -> shop.getStatus() != 0)
                            .collect(Collectors.toList());
                }
            }
            log.info("shop list: {}", list);
            // 根据 shopType 进一步筛选店铺
//            if (param.getShopType() != null) {
//                switch (param.getShopType()) {
//                    case 1: // 到店用 (同城服务)
//                        list = list.stream()
//                                .filter(shop -> shop != null && shop.getStatus() != null && shop.getStatus() == 1)
//                                .collect(Collectors.toList());
//                        break;
//                    case 2: // 电商购 (线上商城)
//                        list = list.stream()
//                                .filter(shop -> shop != null && shop.getStatus() != null && shop.getStatus() == 2|| shop.getStatus()==3)
//                                .collect(Collectors.toList());
//                        break;
//                    case 3: // 同城配送
//                        list = list.stream()
//                                .filter(shop -> shop != null && shop.getStatus() != null && shop.getStatus() == 3)
//                                .collect(Collectors.toList());
//                        break;
//                    default:
//                        // 默认不过滤，返回所有店铺
//                        break;
//                }
//            }

//            System.out.println(list);

//            根据店铺创建时间进行排序
            if (param.getTimeOrder() != null) {
                if (param.getTimeOrder() == 1) {
                    list = list.stream()
                            .sorted(Comparator.comparing(CereShopCommentVO::getCurrentTime))
                            .collect(Collectors.toList());
                } else if (param.getTimeOrder() == 2) {
                    list = list.stream()
                            .sorted(Comparator.comparing(CereShopCommentVO::getCurrentTime).reversed())
                            .collect(Collectors.toList());
                }
            }

            // 根据是否全天筛选
            if (param.getAllDays() != null) {
                if (param.getAllDays() == 1) {
                    list = list.stream()
                            .filter(shop -> shop != null && shop.getAllDayToUse() != null && shop.getAllDayToUse() == 1)
                            .collect(Collectors.toList());
                } else {
                    list = list.stream()
                            .filter(shop -> shop != null && shop.getAllDayToUse() != null && shop.getAllDayToUse() != 1)
                            .collect(Collectors.toList());
                }
            }
            System.out.println(list);
        }


        //     对用用户输入的距离进行筛选
        if (param.getDistance() != null) {
            list = list.stream()
                    .filter(product -> product.getUserAndShopDistance() != null
                            && product.getUserAndShopDistance() <= param.getDistance())
                    .collect(Collectors.toList());
        }


        if (list == null) {
            return null;
        }

        PageInfo<CereShopCommentVO> pageInfo = new PageInfo<>(list);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }


    public String getCityName(String cityCode) {

        // 使用 QueryWrapper 构造查询条件
        QueryWrapper<CereCityDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adcode", cityCode);
        queryWrapper.select("name"); // 只查询 name 字段

        CereCityDetail cityDetail = cityDAO.selectOne(queryWrapper);
        return cityDetail != null ? cityDetail.getName() : null;
    }


    @Override
    public void updateState(CerePlatformShop cerePlatformShop) throws CoBusinessException {
        cerePlatformShopDAO.updateState(cerePlatformShop);
    }

    @Override
    public void insert(CerePlatformShop cerePlatformShop) throws CoBusinessException {
        cerePlatformShopDAO.insert(cerePlatformShop);
    }

    @Override
    public void update(CerePlatformShop cerePlatformShop) throws CoBusinessException {
        cerePlatformShopDAO.updateByPrimaryKeySelective(cerePlatformShop);
    }

    @Override
    public PlatformShop getById(Long shopId) throws CoBusinessException {
        //查询店铺信息
        PlatformShop shop = cerePlatformShopDAO.findShop(shopId);
        if (shop != null) {
            //查询店铺退货地址信息
            CereShopReturn cereShopReturn = cereShopReturnService.findByShopId(shopId);
            shop.setShopReturn(cereShopReturn);
            //查询认证数据
            if (IntegerEnum.getByName("主体类型-个人").equals(shop.getAuthenType())) {
                //个人认证
                shop.setPersonal(cereShopPersonalService.findByShopId(shopId));
            } else if (IntegerEnum.getByName("主体类型-个体工商户").equals(shop.getAuthenType())) {
                //个体工商户户认证
                shop.setIndividualBusinesses(cereShopIndividualBusinessesService.findByShopId(shopId));
            } else if (IntegerEnum.getByName("主体类型-企业").equals(shop.getAuthenType())) {
                //企业认证
                shop.setEnterprise(cereShopEnterpriseService.findByShopId(shopId));
            } else if (IntegerEnum.getByName("主体类型-其他组织").equals(shop.getAuthenType())) {
                //其他组织认证
                shop.setOtherOrganizations(cereShopOtherOrganizationsService.findByShopId(shopId));
            } else if (IntegerEnum.BRAND.getCode().equals(shop.getAuthenType())) {
                //品牌旗舰店认证
                shop.setBrand(cereShopBrandService.findByShopId(shopId));
            }
        }
        return shop;
    }

    @Override
    public CerePlatformShop findByShopName(String shopName) {
        return cerePlatformShopDAO.findByShopName(shopName);
    }

    @Override
    public CerePlatformShop findByPhone(String shopPhone) {
        return cerePlatformShopDAO.findByPhone(shopPhone);
    }

    @Override
    public CerePlatformShop checkShopIdByName(String shopName, Long shopId) {
        return cerePlatformShopDAO.checkShopIdByName(shopName, shopId);
    }

    @Override
    public CerePlatformShop checkShopIdByPhone(String shopPhone, Long shopId) {
        return cerePlatformShopDAO.checkShopIdByPhone(shopPhone, shopId);
    }

    @Override
    public CerePlatformShop check(String shopPhone) {
        return cerePlatformShopDAO.check(shopPhone);
    }

    @Override
    public ShopIndex getIndex(ShopParam param, CereBuyerUser user) throws CoBusinessException, Exception {
        String time = TimeUtils.yyMMddHHmmss();
        //查询店铺数据
        ShopIndex index = cerePlatformShopDAO.findShopIndex(param.getShopId());
        if (index != null) {
            //查询店铺粉丝数
            index.setFansNumber(cereBuyerCollectService.findShopCollectCount(index.getShopId()));
            //查询店铺商品总类数
            index.setClassifyNumber(cereShopProductService.findClassifyNumber(param.getShopId()).size());
            //查询店铺已售件数
            index.setNumber(cerePlatformShopDAO.findShopNumber(param.getShopId()));
            if (user != null) {
                //默认未收藏
                index.setIfCollect(IntegerEnum.NO.getCode());
                //查询收藏id
                CereBuyerCollect cereBuyerCollect = cereBuyerCollectService.findByUserShopId(user.getBuyerUserId(), index.getShopId());
                if (cereBuyerCollect != null) {
                    index.setCollectId(cereBuyerCollect.getCollectId());
                    index.setIfCollect(cereBuyerCollect.getState());
                }
            }
            //查询店铺优惠券数据
            List<ShopCoupon> coupons = cerePlatformShopDAO.findShopCoupons(param.getShopId());
            if (!EmptyUtils.isEmpty(coupons) && user != null) {
                if (user != null) {
                    //设置优惠券状态
                    for (int i = 0; i < coupons.size(); i++) {
                        if (i < 0) {
                            i = 0;
                        }
                        ShopCoupon shopCoupon = coupons.get(i);
                        //查询用户已领取的优惠券总数
                        int count = cereBuyerShopCouponService.findCount(user.getBuyerUserId(), shopCoupon.getShopCouponId());
                        if (count <= 0 && shopCoupon.getStockNumber() <= 0) {
                            //未领取过且库存数量为0，过滤这个优惠券
                            coupons.remove(i);
                            i--;
                            continue;
                        }
                        if (!EmptyUtils.isEmpty(shopCoupon.getFrequency())) {
                            //如果有限制领取次数
                            if (shopCoupon.getFrequency() == count) {
                                //如果限制领取次数=用户已领取优惠券总数,修改店铺优惠券状态为已领取
                                shopCoupon.setState(IntegerEnum.COUPON_HAVE.getCode());
                            }
                        }
                    }
                } else {
                    //未登录
                    for (int i = 0; i < coupons.size(); i++) {
                        if (i < 0) {
                            i = 0;
                        }
                        ShopCoupon shopCoupon = coupons.get(i);
                        if (shopCoupon.getStockNumber() <= 0) {
                            //如果库存小于等于0,过滤优惠券
                            coupons.remove(i);
                            i--;
                        }
                    }
                }
            }
            index.setCoupons(coupons);
            //查询拼团专区数据
            List<ShopGroupWork> shopGroupWork = cerePlatformShopDAO.findGroupWork(param.getShopId());
            if (!EmptyUtils.isEmpty(shopGroupWork)) {
                shopGroupWork.forEach(work -> {
                    //查询拼团专区商品数据
                    work.setGroupProducts(cerePlatformShopDAO.findGroupProducts(work.getShopGroupWorkId()));
                });
            }
            index.setShopGroupWork(shopGroupWork);
            //查询秒杀专区数据
            List<ShopSeckill> shopSeckill = cerePlatformShopDAO.findSeckill(param.getShopId());
            if (!EmptyUtils.isEmpty(shopSeckill)) {
                for (ShopSeckill seckill : shopSeckill) {
                    //查询秒杀专区商品数据
                    seckill.setSeckillProducts(cerePlatformShopDAO.findSeckillProducts(seckill.getShopSeckillId()));
                    //设置活动倒计时时间
                    seckill.setTime(TimeUtils.getCountDownByTime(time, seckill.getEffectiveEnd()));
                }
            }
            index.setShopSeckill(shopSeckill);
            //查询限时折扣专区数据
            List<ShopDiscount> shopDiscount = cerePlatformShopDAO.findDiscount(param.getShopId());
            if (!EmptyUtils.isEmpty(shopDiscount)) {
                for (ShopDiscount discount : shopDiscount) {
                    //查询限时折扣专区商品数据
                    discount.setDiscountProducts(cerePlatformShopDAO.findDiscountProducts(discount.getShopDiscountId()));
                    //设置活动倒计时时间
                    discount.setTime(TimeUtils.getCountDownByTime(time, discount.getEndTime()));
                }
            }
            index.setShopDiscount(shopDiscount);
        }
        return index;
    }

    @Override
    public String getSharePic(ShopPosterParam param, CereBuyerUser user) throws CoBusinessException {
        if (user == null) {
            throw new CoBusinessException(CoReturnFormat.USER_NOT_LOGIN);
        }
        try {
            PlatformShop shop = getById(param.getShopId());
            int env = param.getTerminal();

            ShopParam shopParam = new ShopParam();
            shopParam.setShopId(param.getShopId());
            PageHelper.startPage(1, 8);
            List<Product> list = cerePlatformShopDAO.getShopProducts(shopParam);
            if (!EmptyUtils.isEmpty(list)) {
                list.forEach(a -> {
                    //查询付款人数
                    a.setUsers(findPayUsers(a.getProductId()));
                    //设置活动相关信息
                    ProductBo productBo = cereShopProductService.fetchProductCache(a.getShopId(), a.getProductId(), a.getSkuId(), user, false);
                    if (productBo != null) {
                        a.setActivityType(productBo.getActivityType());
                        a.setOriginalPrice(productBo.getOriginalPrice());
                        a.setPrice(productBo.getPrice());
                        a.setTotal(productBo.getTotal());
                    }
                });
            }

            String shareHeadUrl = user.getHeadImage();
            String shareName = user.getName();
            if (StringUtils.isBlank(shareName)) {
                shareName = user.getWechatName();
            }
            String invitationCode = null;
            if (param.getDistributorId() != null) {
                invitationCode = cereShopDistributorService.findInvitationCode(param.getDistributorId());
            }

            String accessToken = WechatUtil.getAccessToken();
            return genPoster(env, param.getType(), shop.getShopId(), shareHeadUrl, shareName, shop.getShopBrief(), invitationCode, list, accessToken, param.getDistributorId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断店铺是否为新店
     *
     * @param createTime 店铺创建时间戳（毫秒）
     * @return true:新店 false:非新店
     */
    public static Integer isNewShop(long createTime) {
        // 获取当前时间戳
        long currentTime = System.currentTimeMillis();

        // 计算一周的毫秒数
        long oneWeekInMillis = 7 * 24 * 60 * 60 * 1000L;

        // 判断创建时间是否在一周内
        return (currentTime - createTime) <= oneWeekInMillis ? 1 : 0;
    }

    @Override
    public PageInfo<ProductVo> productHot(PageParam param) {
        // 设置分页参数
        if (param.getPage() == null || param.getPageSize() == null) {
            param.setPage(1);
            param.setPageSize(10);
        }
        PageHelper.startPage(param.getPage(), param.getPageSize());

        // 查询店铺热榜商品数据
        List<ProductVo> shops = cerePlatformShopDAO.findHotProducts(param);
        return new PageInfo<>(shops);

    }

    /**
     * 生成分享海报
     *
     * @param env            1-APP 2-微信小程序 3-H5 4-支付宝小程序
     * @param shopId
     * @param shareHeadUrl
     * @param shareName
     * @param shopBrief
     * @param invitationCode
     * @param productList
     * @param accessToken
     * @param distributorId  分销员id
     */
    private String genPoster(int env, Integer type, Long shopId, String shareHeadUrl, String shareName,
                             String shopBrief, String invitationCode, List<Product> productList,
                             String accessToken, Long distributorId) throws Exception {
        int width = 640;
        int height = 1220;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = image.createGraphics();

        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.OPAQUE);
        g2d.dispose();
        g2d = image.createGraphics();

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        g2d.setColor(new Color(182, 249, 225));
        g2d.setStroke(new BasicStroke(1));
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        BufferedImage shareHeadImg = ImageIO.read(new URL(shareHeadUrl));

        //把图片切成一个圓
        {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
            Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
            g2d.setStroke(s);
            //留一个像素的空白区域，这个很重要，画圆的时候把这个覆盖
            int border = 1;
            //图片是一个圆型
            Ellipse2D.Double shape = new Ellipse2D.Double(20, 40, 120 - border * 2, 120 - border * 2);
            //需要保留的区域
            g2d.setClip(shape);
            g2d.drawImage(shareHeadImg.getScaledInstance(118, 118, Image.SCALE_SMOOTH), 20, 40, 120 - border * 2, 120 - border * 2, null);
            g2d.dispose();
        }

        g2d = image.createGraphics();
        g2d.setStroke(new BasicStroke(1));
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        //分享人名称

        g2d.setPaint(Color.BLACK);
        g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 21));
        g2d.drawString(shareName, 170, 92);

        //店铺简介
        if (shopBrief != null) {
            g2d.setPaint(new Color(194, 194, 194));
            g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 24));
            FontMetrics fm2 = g2d.getFontMetrics(g2d.getFont());
            while (fm2.stringWidth(shopBrief) > 450) {
                shopBrief = shopBrief.substring(0, shopBrief.length() - 1);
            }
            g2d.drawString(shopBrief, 170, 150);
        }

        //邀请码 邀请下级的时候才需要展示
        if (type != null && type == 2) {
            g2d.setPaint(new Color(249, 220, 116));
            g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 24));
            g2d.drawString("邀请码: " + invitationCode, 375, 95);
        }

        //商品列表背景
        g2d.setPaint(new Color(244, 245, 247));
        g2d.setBackground(new Color(244, 245, 247));
        g2d.clearRect(16, 186, width - 16 * 2, 790);

        //商品列表
        if (!CollectionUtils.isEmpty(productList)) {
            int count = 0;
            int productHeight = 368;
            g2d.setBackground(Color.WHITE);
            for (Product product : productList) {
                int marginLeft = 338;
                if (count % 2 == 0) {
                    marginLeft = 71;
                }
                int marginTop = count / 2 * (productHeight + 16) + 206;
                int productImgWidth = 231;
                int productImgHeight = 231;
                g2d.clearRect(marginLeft, marginTop, productImgWidth, productHeight);

                BufferedImage proImg = readImgInner(product.getProductImage());
                if (proImg == null) {
                    continue;
                }
                g2d.drawImage(proImg.getScaledInstance(productImgWidth, productImgHeight, Image.SCALE_SMOOTH), marginLeft, marginTop, null);

                //间距20

                //商品名称
                FontMetrics fmPro = g2d.getFontMetrics(g2d.getFont());
                g2d.setPaint(Color.BLACK);
                g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 18));
                String productName = product.getProductName();
                while (fmPro.stringWidth(productName) > 150 && productName.length() > 1) {
                    productName = productName.substring(0, productName.length() - 1);
                }
                //文字位置是从底部算起的，所以加上间距 还要加上字体本身
                g2d.drawString(productName, marginLeft + 12, marginTop + productImgHeight + 20 + 18);

                //间距16

                //多少人付款
                g2d.setPaint(new Color(236, 179, 30));
                g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 15));
                g2d.drawString(product.getUsers() + "人付款", marginLeft + 12, marginTop + productImgHeight + 20 + 18 + 16 + 15);

                //间距22

                //售价 和 原价
                g2d.setPaint(new Color(225, 37, 27));
                g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 16));
                g2d.drawString("￥" + product.getPrice(), marginLeft + 12, marginTop + productImgHeight + 20 + 18 + 16 + 15 + 22 + 16);

                FontMetrics priceFm = g2d.getFontMetrics(g2d.getFont());
                int priceWidth = priceFm.stringWidth("￥" + product.getPrice());

                g2d.setPaint(Color.GRAY);
                g2d.setFont(new Font("Alibaba PuHuiTi R", Font.PLAIN, 13));
                FontMetrics originalPriceFm = g2d.getFontMetrics(g2d.getFont());
                int originalPriceWidth = originalPriceFm.stringWidth("￥" + product.getOriginalPrice());
                int originalPriceMarginLeft = marginLeft + 12 + priceWidth + 24;
                int originalPriceMarginTop = marginTop + productImgHeight + 20 + 18 + 16 + 15 + (22 + 4) + 13;
                g2d.drawString("￥" + product.getOriginalPrice(), originalPriceMarginLeft, originalPriceMarginTop);
                //画删除线
                int delLineMarginTop = originalPriceMarginTop - g2d.getFont().getSize() / 2;
                g2d.drawLine(originalPriceMarginLeft - 2, delLineMarginTop, originalPriceMarginLeft + originalPriceWidth + 4, delLineMarginTop);
                count++;
                if (count == 4) {
                    break;
                }
            }
        }

        //生成底部文案 和 二维码
        g2d.setPaint(Color.BLACK);
        g2d.setFont(new Font("Alibaba PuHuiTi R", Font.BOLD, 17));
        BufferedImage qrCode = null;
        if (env == 1 || env == 2) {
            g2d.drawString("长按识别小程序", 30, 1036);
            String url = "pages_category_page1/store/index?storeId=" + shopId + "&salesId=" + distributorId;
            if (type != null && type == 2) {
                url = "pages_category_page1/distributionModule/recruit?shopId=" + shopId + "&invitationCode=" + invitationCode;
            }
            qrCode = WechatUtil.getWxacode(accessToken, url);
        } else if (env == 3) {
            g2d.drawString("扫描二维码购买商品", 30, 1036);
            String url = shopUrl + "storeId=" + shopId + "&salesId=" + distributorId;
            if (type != null && type == 2) {
                url = recruitUrl + "shopId=" + shopId + "&invitationCode=" + invitationCode;
            }
            qrCode = QRCodeUtil.createImage(url, null, false);
        } else if (env == 4) {
            g2d.drawString("长按识别小程序", 30, 1036);
            String url = "pages_category_page1/store/index";
            String reqParam = "storeId=" + shopId + "&salesId=" + distributorId;
            if (type != null && type == 2) {
                url = "pages_category_page1/distributionModule/recruit";
                reqParam = "shopId=" + shopId + "&invitationCode=" + invitationCode;
            }
            String qrCodeUrl = AlipayUtil.generateQrCode(url, reqParam);
            qrCode = ImageIO.read(new URL(qrCodeUrl));
            qrCode = qrCode.getSubimage(102, 134, 1336, 1336);
        }
        if (qrCode != null) {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            g2d.drawImage(qrCode, 390, 976, 240, 240, null);
        }

        g2d.dispose();

        String fileName = UUID.randomUUID().toString() + ".png";
        InputStream inputStream = bufferedImageToInputStream(image);
        ByteArrayOutputStream baos = cloneInputStream(inputStream);
        return fileStrategy.uploadStream(fileName, IOUtils.toByteArray(new ByteArrayInputStream(baos.toByteArray())), new ByteArrayInputStream(baos.toByteArray()), inputStream.available());
    }

    private BufferedImage readImgInner(String imageUrl) throws Exception {
        if (StringUtil.isNotBlank(imageUrl)) {
            if (imageUrl.endsWith(".webp")) {
                // Obtain a WebP ImageReader instance
                ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

                // Configure decoding parameters
                WebPReadParam readParam = new WebPReadParam();
                readParam.setBypassFiltering(true);

                // Configure the input on the ImageReader
                reader.setInput(ImageIO.createImageInputStream(new URL(imageUrl).openStream()));

                // Decode the image
                return reader.read(0, readParam);
            } else {
                return ImageIO.read(new URL(imageUrl));
            }
        }
        return null;
    }

    private static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将BufferedImage转换为InputStream
     *
     * @param image
     * @return
     */
    public InputStream bufferedImageToInputStream(BufferedImage image) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IPage<ShopInfo> getShopInfoListByParam(ShopQueryParam param) {
        //好评优先
        if (param.getShopType() == null) {

            param.setShopType(1);
        }
        IPage<ShopInfo> page = cerePlatformShopDAO.findSalesShopByParam(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(param.getPageNum(), param.getPageSize()), param);
        return page;
    }

    @Override
    public ShopInfo getShopInfo(Long shopId, CereBuyerUser user) throws CoBusinessException, ParseException {
        String now = TimeUtils.yyMMddHHmmss();
        ShopInfo shopInfo = cerePlatformShopDAO.getShopInfo(shopId, user.getBuyerUserId());
        if (shopInfo == null) {
            throw new CoBusinessException(CoReturnFormat.MERCHANT_NOT_EXIST);
        }
        //处理店铺轮播图
        if (StrUtil.isNotBlank(shopInfo.getShopImage())) {
            shopInfo.setShopImages(Arrays.asList(shopInfo.getShopImage().split(",")));
        }
        //判断店铺标签
        ArrayList<String> shopLabels = new ArrayList<>();
        if (shopInfo.getAuthenType() == null) {
            throw new CoBusinessException(CoReturnFormat.MERCHANT_NOT_AUTH);
        }
        shopLabels.add(BusinessTypeEnum.getMessageByCode(shopInfo.getAuthenType()));
        String beforeTime = TimeUtils.getMoreDayAfter(now, -7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = format.parse(shopInfo.getCreateTime());
        Date date2 = format.parse(beforeTime);
        if (date1.after(date2) || date1.equals(date2)) {
            shopLabels.add("新店");
        }
        shopInfo.setShopLabels(shopLabels);
        // 手动处理 shopWeeks 字符串转换为列表
        if (StrUtil.isNotBlank(shopInfo.getShopWeeks())) {
            List<String> shopWeeksList = Arrays.asList(shopInfo.getShopWeeks().split(","));
            shopWeeksList = shopWeeksList.stream().map(String::trim).collect(Collectors.toList());
            shopInfo.setShopWeekList(shopWeeksList);
            String weekStr = BusinessWeeksUtils.mergeWeeks(shopWeeksList);
            String timeStr = shopInfo.getStartTime() + "-" + shopInfo.getEndTime();
            shopInfo.setShopWeeks(weekStr + " " + timeStr);
        }
        //写死标签
        shopInfo.setServiceLabels(Arrays.asList("热销店铺", "全周可用", "价格实惠", "好评如潮"));
        return shopInfo;
    }

    @Override
    public Integer getAuthType(Long shopId) {
        return cerePlatformShopDAO.getAuthType(shopId);
    }

    @Override
    public PageInfo<ShopVo> shopHot(PageParam param) {
        // 设置分页参数
        if (param.getPage() == null || param.getPageSize() != null) {
            param.setPage(1);
            param.setPageSize(10);
        }

        PageHelper.startPage(param.getPage(), param.getPageSize());

        // 查询店铺热榜商品数据
        List<ShopVo> shops = cerePlatformShopDAO.findHotShop();
        return new PageInfo<>(shops);
    }



}
